package com.right.health.app.ui.me.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.magic.basic.adapter.BaseAbsAdapter;
import com.magic.basic.utils.TimeUtil;
import com.right.health.app.R;
import com.right.health.app.bean.OrderResponse;
import com.right.health.app.constants.AppConfig;

/*
 * Created by hopeliao on 2019-09-24
 *
 */
public class MeReserveAdapter extends BaseAbsAdapter<OrderResponse.OrderInfo> {

    private View.OnClickListener mClickListener;

    private String mFormat;

    public void setOnClickListener(View.OnClickListener listener) {
        this.mClickListener = listener;
    }

    public MeReserveAdapter(Context context) {
        super(context);
//        mFormat = context.getString(R.string.format_order);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.me_reserve_item, null);
            viewHolder = new ViewHolder();
            viewHolder.contentImage = (ImageView) convertView.findViewById(R.id.iv_avatar);
            viewHolder.nameTv = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.orderTv = (TextView) convertView.findViewById(R.id.tv_order);
            viewHolder.levelTv = (TextView) convertView.findViewById(R.id.tv_level);
            viewHolder.departmentTv = (TextView) convertView.findViewById(R.id.tv_department);
            viewHolder.dateTv = (TextView) convertView.findViewById(R.id.tv_day);

            viewHolder.modifyBtn = (Button) convertView.findViewById(R.id.btn_modify);
            viewHolder.cancelBtn = (Button) convertView.findViewById(R.id.btn_cancel);


            if (mClickListener != null) {
                viewHolder.modifyBtn.setOnClickListener(mClickListener);
                viewHolder.cancelBtn.setOnClickListener(mClickListener);
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.modifyBtn.setTag(position);
        viewHolder.cancelBtn.setTag(position);

        OrderResponse.OrderInfo orderInfo = mDataSource.get(position);

//        viewHolder.orderTv.setText(String.format(mFormat, orderInfo.orderid));
        viewHolder.orderTv.setText(orderInfo.orderid);
        viewHolder.nameTv.setText(orderInfo.doctorname);
        viewHolder.levelTv.setText(orderInfo.doctorlevel);
        viewHolder.departmentTv.setText(orderInfo.deptname);

        viewHolder.cancelBtn.setText("取消");
        viewHolder.cancelBtn.setClickable(true);
        viewHolder.cancelBtn.setBackgroundResource(R.drawable.selector_cancel_btn);
        //订单状态(1:待支付,2:已支付,3:支付失败,4.已取消),
        if (orderInfo.orderstatus.equals(OrderResponse.ORDER_STATE_NOT_PAY)) {
            viewHolder.modifyBtn.setVisibility(View.VISIBLE);
        } else if (orderInfo.orderstatus.equals(OrderResponse.ORDER_STATE_PAID)) {
            viewHolder.modifyBtn.setVisibility(View.VISIBLE);
           viewHolder.modifyBtn.setText("已支付");
           viewHolder.modifyBtn.setClickable(false);
            viewHolder.cancelBtn.setText("取消");

        } else if (orderInfo.orderstatus.equals(OrderResponse.ORDER_STATE_ERROR)) {
            viewHolder.modifyBtn.setVisibility(View.INVISIBLE);
            viewHolder.cancelBtn.setClickable(false);
            viewHolder.cancelBtn.setBackgroundResource(R.drawable.shape_cancel_btn1);
            viewHolder.cancelBtn.setText("支付失败");

        } else if (orderInfo.orderstatus.equals(OrderResponse.ORDER_STATE_CANCEL)) {
            viewHolder.modifyBtn.setVisibility(View.INVISIBLE);
            viewHolder.cancelBtn.setClickable(false);
            viewHolder.cancelBtn.setBackgroundResource(R.drawable.shape_cancel_btn1);
            viewHolder.cancelBtn.setText("取消");
        } else if (orderInfo.orderstatus.equals(OrderResponse.ORDER_STATE_COMPLETE)) {
            viewHolder.modifyBtn.setVisibility(View.INVISIBLE);
            viewHolder.cancelBtn.setClickable(false);
            viewHolder.cancelBtn.setBackgroundResource(R.drawable.shape_cancel_btn1);
            viewHolder.cancelBtn.setText("已完成");
        }

        String time = TimeUtil.convertTime(AppConfig.FORMAT_TIME, orderInfo.regdate);
        viewHolder.dateTv.setText(time + " " + orderInfo.shiftname);
        return convertView;
    }

    private class ViewHolder {
        private ImageView contentImage;
        private TextView nameTv, orderTv, levelTv, departmentTv, dateTv;
        private Button modifyBtn, cancelBtn;
    }
}
